//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Q4Tech.Engineering.UpdaterAgent.Management;
using Microsoft.WindowsMobile.Status;

namespace Q4Tech.Engineering.UpdaterAgent.UpdaterView
{
    public partial class UpdaterView : Form, IUpdaterView
    {

        public event EventHandler ForceUpdate;
        public event EventHandler ViewDeviceID;
        public event EventHandler<BoolEventArgs> VerboseCheck;
        public event EventHandler LoadView;
        public event EventHandler<ChangeEventArgs> OrientationChange;
        public event EventHandler<BoolEventArgs> ActiveView;

        private SystemState orientationProperty = new SystemState(SystemProperty.DisplayRotation);

        public UpdaterView()
        {
            InitializeComponent();
        }

        private void UpdaterView_Load(object sender, EventArgs e)
        {
            if (orientationProperty != null)
                orientationProperty.Changed += orientationProperty_Changed;

            if (this.LoadView != null && !IsDesignMode)
                LoadView(sender, e);
        }

        private void UpdaterView_Activated(object sender, EventArgs e)
        {
            if (ActiveView != null)
                ActiveView(sender, new BoolEventArgs(true));
        }

        private void UpdaterView_Deactivate(object sender, EventArgs e)
        {
            if (ActiveView != null)
                ActiveView(sender, new BoolEventArgs(false));
        }

        void orientationProperty_Changed(object sender, ChangeEventArgs args)
        {
            if (OrientationChange != null)
                OrientationChange(sender, args);
        }

        private void forceUpdate_Click(object sender, EventArgs e)
        {
            if (ForceUpdate != null)
                ForceUpdate(sender, e);
        }

        private void viewDeviceID_Click(object sender, EventArgs e)
        {
            if (ViewDeviceID != null)
                ViewDeviceID(sender, e);
        }

        private void verboseCheck_CheckStateChanged(object sender, EventArgs e)
        {
            if (VerboseCheck != null)
                VerboseCheck(sender, new BoolEventArgs(this.verboseCheck.Checked));
        }

        private void closeMenu_Click(object sender, EventArgs e)
        {
            if (orientationProperty != null)
            {
                orientationProperty.Changed -= orientationProperty_Changed;
                orientationProperty.Dispose();
                orientationProperty = null;
            }

            Application.Exit();
        }


        #region IUpdaterView Members

        public void WriteLine(string message)
        {
            UpdateUI(delegate
            {
                txtMessages.Text = "> " + message + "\r\n" +
                    (txtMessages.Text.Length > 2 ? "- " + txtMessages.Text.Substring(2) : "");
                this.Invalidate();
            });
        }

        public void SetStatus(bool connected, int pendingDownloads)
       {
           UpdateUI(delegate
           {
               if (connected)
               {
                   if (pendingDownloads == 0)
                   {
                       statusPicture.Image = Properties.Resources.PocketPortraitConnected;
                       statusLabel.Text = Properties.Resources.Connected;
                       forceUpdate.Enabled = true;
                   }
                   else
                   {
                       statusPicture.Image = Properties.Resources.PocketPortraitDownloading;
                       statusLabel.Text = Properties.Resources.Downloading;
                       forceUpdate.Enabled = false;
                   }
               }
               else
               {
                   statusPicture.Image = Properties.Resources.PocketPortraitDisconnected;
                   statusLabel.Text = Properties.Resources.Disconnected;
                   forceUpdate.Enabled = false;
               }
               this.Update();
               this.Refresh();
           });
       }

        public bool ForceUpdateEnabled
        {
            set { UpdateUI(delegate { forceUpdate.Enabled = value; }); }
        }

        public string MessageLog
        {
            get { return this.txtMessages.Text; }
            set { this.txtMessages.Text = value; }
        }

        public bool VerboseSet
        {
            set { this.verboseCheck.Checked = value; }
        }

        #endregion

        private void UpdateUI(EventHandler action)
        {
            this.Invoke(action);
        }

        private bool IsDesignMode
        {
            get { return Site != null && Site.DesignMode; }
        }





   

    }
}
